package com.example.forum.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


public class UploadUtil {
    //阿里域名
    public static final String ALI_DOMAIN = "https://dyk-image.oss-cn-beijing.aliyuncs.com/";
    public static String uploadImage(MultipartFile file) throws IOException {
        //生成文件名
        String originalFileName = file.getOriginalFilename();
        String ext = "."+ FilenameUtils.getExtension(originalFileName);
        String uuid = UUIDUtil.UUID_32();
        String fileName  = uuid+ ext;
        //生成地域节点
        String endpoint = "http://oss-cn-beijing.aliyuncs.com";
        String accessKeyId = "LTAI5tHgN9t3FhsANfs6jmzm";
        String accessKeySecret =  "rm7S6RsPXg0RRTrXesbW5cyxQz71MY";
        //OSS客户端对象
        OSS ossClient = new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);
        ossClient.putObject("dyk-image", //仓库名
                fileName,//文件名
                file.getInputStream());
        ossClient.shutdown();
        return ALI_DOMAIN + fileName;
    }
}
